package org.flexlancer.databases
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	public class Suppliers extends EventDispatcher
	{
		public function Suppliers(target:IEventDispatcher=null)
		{
			super(target);
		}
		public static function List(condition:String = ""):Array{
			try{
				return DatabaseManager.Manager.Select("supplier",null,condition);
			}catch(err:Error){
				throw err;
			}
			return null;
		}
		public static function AddSupplier(supplierName:String, supplierInfo:Array):void{
			try{//customerid INTEGER PRIMARY KEY AUTOINCREMENT","customername TEXT", "customertype TEXT", "credit REAL"]);
				DatabaseManager.Manager.Begin();
				var custObj:Object = new Object();
				custObj.suppliername = " '" + supplierName + "' ";
				
				DatabaseManager.Manager.Insert("supplier", custObj);
				var insertID:Number = DatabaseManager.Manager.LastInsertRowID;
				var recs:Array = DatabaseManager.Manager.Select("supplier");
				insertID = recs[insertID-1].supplierid;
				
				if (supplierInfo){
					for each ( var singleContact:Object in supplierInfo ){
						//["refid TEXT","contacttype TEXT", "contactdetail TEXT"]
						var contObj:Object = new Object();
						contObj.refid = " 'SUPL-"+insertID+"' ";
						contObj.contacttype = " '"+singleContact.type+"' ";
						contObj.contactdetail = " '"+singleContact.detail+"' ";
						DatabaseManager.Manager.Insert("supplierinfo",contObj);
					}
				}
				
				DatabaseManager.Manager.Commit();
			}catch(err:Error){
				DatabaseManager.Manager.Rollback();
				throw err;
			}
		}
	}
}